import { SendMessageModalComponent } from './send-message-modal/send-message-modal.component';
import { ReportAddressModalComponent } from './../report-address-modal/report-address-modal.component';
import { SiteDTO, WarnInfoDTO } from './../../../shared/service-proxy/service-proxies';
import { ReportWarnModalComponent } from './report-warn-modal/report-warn-modal.component';
import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { NzTableHelper } from '@shared/helpers/NzTableHelper';
import { finalize } from 'rxjs/operators';
import { ServiceProxy, ContainerDTO, ElectronicLockDTO, PathDTO } from '@shared/service-proxy/service-proxies';
import * as moment from 'moment';

@Component({
  selector: 'app-alarm-management',
  templateUrl: './alarm-management.component.html',
  styleUrls: ['./alarm-management.component.less']
})
export class AlarmManagementComponent extends AppComponentBase implements OnInit {
  @ViewChild('reportWarnModal') reportWarnModal: ReportWarnModalComponent;
  @ViewChild('reportAddressModal') reportAddressModal: ReportAddressModalComponent
  @ViewChild('sendMessageModal') sendMessageModal: SendMessageModalComponent;
  tableHelper: NzTableHelper;
  params: string;
  routeParams: any;
  containers: ContainerDTO[] = [];
  sites: SiteDTO[] = [];
  locks: ElectronicLockDTO[] = [];
  siteId: string = "";
  pathList: PathDTO[] = [];
  dateRange = [];

  constructor(
    injector: Injector,
    private _service: ServiceProxy) {
    super(injector);
    this.tableHelper = new NzTableHelper();
  }

  ngOnInit() {
    this.load();
    this.routeParams = { warnType: "", containerCode: "", keyWord: "",siteId:'', routeId: "", lockCode: "", warnTimeStart: "", warnTimeEnd: "" };
    this.getContainer();
    this.getSites();
    this.getLocks();
  }

  onChange(result: any): void {
    if (result) {
      if (result[0]) {
        this.routeParams.warnTimeStart = moment(result[0]).format('YYYY-MM-DD');
      }
      if (result[1]) {
        this.routeParams.warnTimeEnd = moment(result[1]).format('YYYY-MM-DD');
      }

    }
    console.log('onChange: ', result);
  }

  load(pageIndex?: number) {
    if (typeof pageIndex !== 'undefined') {
      this.tableHelper.pageIndex = pageIndex || 1;
    }
    this.getList();
  }

  sort(sort: { key: string, value: string }): void {
    this.tableHelper.setSorting = sort;
    this.load();
  }

  getList() {
    if (this.routeParams) {
      this.params = JSON.stringify(this.routeParams);
    }
    this._service.searchAllWarnInfoPageUsingGET(this.tableHelper.pageIndex, this.tableHelper.pageSize, this.params
    ).pipe(finalize(() => {
      this.tableHelper.loading = false;
    })).subscribe((result) => {
      this.tableHelper.items = result.result.fieldList;
      this.tableHelper.totalCount = result.result.totalRow;
    });
  }

  reset() {
    this.routeParams = { warnType: "", containerCode: "", siteId:'', keyWord: "", routeId: "", lockCode: "", warnTimeStart: "", warnTimeEnd: "" };
    this.dateRange = [];
    this.pathList = [];
    this.load();
  }

  getSites() {
    this._service.getAllSitesUsingGET().subscribe(result => {
      this.sites = result.result.fieldList;
    })
  }

  getPathBySite() {
    this._service.getPathListBySiteIdUsingGET(this.routeParams.siteId).subscribe(result => {
      this.pathList = result.result.fieldList;
    })
  }

  getContainer() {
    this._service.getAllContainerDTOUsingGET().subscribe(result => {
      this.containers = result.result.fieldList;
    })
  }

  getLocks() {
    this._service.getAllElectronicLocksUsingGET().subscribe(result => {
      this.locks = result.result.fieldList;
    })
  }

  showReportModal(id: string, report: string) {
    this.reportWarnModal.show(id, report);
  }

  showAddress(id: string) {
    this.reportAddressModal.show(id);
  }

  showMessage(info: any) {
    this.sendMessageModal.show(info);
  }
}
